package encriptacion;

import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputLabel;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import encriptacion.Decoder.BASE64Decoder;
import encriptacion.Encoder.BASE64Encoder;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.faces.FacesException;
//import sun.misc.BASE64Encoder;
//import sun.misc.BASE64Decoder;


public class Index extends AbstractPageBean
{
    private HtmlInputText claveEncriptar = new HtmlInputText();
    private HtmlInputText claveDesencriptar = new HtmlInputText();
    private HtmlInputText textoDesencriptar = new HtmlInputText();
    private HtmlInputText textoEncriptar = new HtmlInputText();
    private String textoTemporalEncriptado = new String();
    private HtmlOutputLabel outputLabel6 = new HtmlOutputLabel();

    private void _init() throws Exception
    {
    }


    public String button1_action() throws NoSuchAlgorithmException, Exception
    {
        //encrypt(textoEncriptar.getValue().toString());
        encrypt(textoEncriptar.getValue().toString(), claveEncriptar.getValue().toString());
        return null;
    }

    public String button2_action() throws NoSuchAlgorithmException, Exception
    {
        //decrypt();
        decrypt(textoTemporalEncriptado, claveDesencriptar.getValue().toString());
        return null;
    }

      public void encrypt(String text, String key) throws Exception
      {
        byte[] bytesKey = key.getBytes();
        SecretKeySpec spec = new SecretKeySpec(bytesKey,0,16,"AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, spec);       
        text = homogeneizar(text);
        byte[] bytesSalida = new byte[text.length() * 2];
        cipher.doFinal(text.getBytes(),0,text.length(),bytesSalida,0);
        BASE64Encoder enc = new BASE64Encoder();
        textoTemporalEncriptado = enc.encode(bytesSalida);
        outputLabel6.setValue(textoTemporalEncriptado);
      }

      public void decrypt(String text, String key) throws Exception
      {
        byte[] bytesKey = key.getBytes();
        byte[] bytesSalida = new byte[32];
        SecretKeySpec spec = new SecretKeySpec(bytesKey,0,16,"AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, spec);
        BASE64Decoder dec = new BASE64Decoder();
        byte[] bytes = dec.decodeBuffer(text);
        cipher.doFinal(bytes,0,bytes.length,bytesSalida,0);
        textoDesencriptar.setValue(new String(bytesSalida));
      }

    private String homogeneizar(String text)
    {
        String s = new String();
        int leng = text.length();
        int pDecimal = leng % 16;
        s = text;
        for(; pDecimal < 16; pDecimal++)
        {
            s = s + "0";
        }
        leng = s.length();
        return s;
    }

   private void decrypt() throws Exception
  {
    String s = claveDesencriptar.getValue().toString();
    byte[] bytes = s.getBytes();
    SecretKeySpec spec = new SecretKeySpec(bytes,"AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, spec);
    BASE64Decoder dec = new BASE64Decoder();
    textoDesencriptar.setValue(new String(cipher.doFinal(dec.decodeBuffer((textoTemporalEncriptado)))));
  }

  private void encrypt(String text) throws Exception
  {
    String s = claveEncriptar.getValue().toString();
    byte[] bytes = s.getBytes();
    System.out.println("Tamaño de la clave: "+bytes.length);
    SecretKeySpec spec = new SecretKeySpec(bytes,"AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, spec);
    BASE64Encoder enc = new BASE64Encoder();
    textoTemporalEncriptado = enc.encode(cipher.doFinal(text.getBytes()));
    outputLabel6.setValue(textoTemporalEncriptado);
  }
  
    public Index()
    {
    }


    @Override
    public void init()
    {
        super.init();
        try
        {
            _init();
        } 
        catch (Exception e)
        {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException)
                    e: new FacesException(e);
        }
    }

    @Override
    public void preprocess()
    {
    }

    @Override
    public void prerender()
    {
    }

    @Override
    public void destroy()
    {
    }

    //GETTERS Y SETTERS

    public HtmlInputText getClaveEncriptar()
    {
        return claveEncriptar;
    }

    public void setClaveEncriptar(HtmlInputText hit)
    {
        this.claveEncriptar = hit;
    }

    public HtmlInputText getTextoEncriptar()
    {
        return textoEncriptar;
    }

    public void setTextoEncriptar(HtmlInputText hit)
    {
        this.textoEncriptar = hit;
    }
    
    public HtmlInputText getTextoDesencriptar()
    {
        return textoDesencriptar;
    }

    public void setTextoDesencriptar(HtmlInputText hit)
    {
        this.textoDesencriptar = hit;
    }

    public HtmlInputText getClaveDesencriptar()
    {
        return claveDesencriptar;
    }

    public void setClaveDesencriptar(HtmlInputText hit)
    {
        this.claveDesencriptar = hit;
    }

        public HtmlOutputLabel getOutputLabel6()
    {
        return outputLabel6;
    }

    public void setOutputLabel6(HtmlOutputLabel hol)
    {
        this.outputLabel6 = hol;
    }
}

